<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui">
<h:body>
	<ui:composition template="/template/template.xhtml">

		<ui:define name="content">
			<h:form id="userForm">
				<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" globalOnly="true" />  
				<p:panel id="userTablePanel">
					<p:dataTable id="userTable" var="item" paginator="true" rows="5"
						paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink}
											{PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
						value="#{mbConfiguration.users}" rowKey="#{item.id}"
						selection="#{mbConfiguration.selectedUser}" selectionMode="single">

						<p:ajax event="rowSelect"
							listener="#{mbConfiguration.onRowUserSelect}"
							update=":userForm:commandPanel,:editUserDlg">
						</p:ajax>

						<p:column>
							<f:facet name="header">
								<h:outputText value="Login" />
							</f:facet>
							<h:outputText value="#{item.login}" />
						</p:column>

						<p:column style="display: none">
							<f:facet name="header">
								<h:outputText value="Password" />
							</f:facet>
							<h:outputText value="#{item.pwd}" />
						</p:column>

						<p:column style="display: none">
							<f:facet name="header">
								<h:outputText value="Name" />
							</f:facet>
							<h:outputText value="#{item.name}" />
						</p:column>


						<p:column>
							<f:facet name="header">
								<h:outputText value="Email" />
							</f:facet>
							<h:outputText value="#{item.email}" />
						</p:column>

						<p:column>
							<f:facet name="header">
								<h:outputText value="Enabled" />
							</f:facet>
							<h:outputText value="#{item.enabled}" />
						</p:column>

						<p:column>
							<f:facet name="header">
								<h:outputText value="Role" />
							</f:facet>
							<h:outputText value="#{item.role}" />
						</p:column>

					</p:dataTable>
					<h:panelGrid id="commandPanel" columns="4">
						<p:commandButton id="addUser" value="Add"
							disabled="#{mbConfiguration.adminRole == false}"
							update=":editUserDlg"
							action="#{mbConfiguration.prepareAddUser}"
							oncomplete="editUserDlgWidget.show()"></p:commandButton>
						<p:commandButton id="editUser" value="Edit"
							action="#{mbConfiguration.prepareEditUser}"
							update=":editUserDlg"
							disabled="#{mbConfiguration.editUserMode == false || mbConfiguration.adminRole == false}"
							oncomplete="editUserDlgWidget.show()"></p:commandButton>
						<p:commandButton id="deleteUser" value="Delete"
							action="#{mbConfiguration.deleteUser}"
							update="userTable,commandPanel"
							disabled="#{mbConfiguration.adminRole == false || mbConfiguration.editUserMode == false}">
						</p:commandButton>
						
						<p:commandButton id="resetPassword" value="Reset Password"
							action="#{mbConfiguration.resetPassword}"
							update="userTable,commandPanel"
							disabled="#{mbConfiguration.adminRole == false || mbConfiguration.editUserMode == false}">
						</p:commandButton>
					</h:panelGrid>
				</p:panel>

			</h:form>

			<script type="text/javascript">
				function handleUserRequest(xhr, status, args) {
					if (args.validationFailed) {
						jQuery('#editUserDlgWidget').effect("shake", {
							times : 3
						}, 100);
					} else {
						editUserDlgWidget.hide();
					}
				}
			</script>
			<p:growl id="growl" showDetail="true" sticky="true" />
			<p:dialog id="editUserDlg"
				header="#{mbConfiguration.editUserMode == true ? 'Edit User' : 'Add User'}"
				minWidth="500" widgetVar="editUserDlgWidget" resizable="false">
				<p:ajax event="close" update=":userForm:userTablePanel"
					listener="#{mbConfiguration.handleClose}" />
				<h:form id="userEditForm">
					<p:messages id="msg" showDetail="true" autoUpdate="true" closable="true" globalOnly="true" />
					<h:panelGrid columns="2" style="margin-bottom:10px">
						<h:outputLabel for="login" value="User Name(*) " />
						<p:inputText id="login"
							value="#{mbConfiguration.selectedUser.login}" required="true"
							label="User Name" size="50" />

						<h:outputLabel for="password" value="Password(*) "
							rendered="#{mbConfiguration.editUserMode == false}" />
						<p:password id="password"
							rendered="#{mbConfiguration.editUserMode == false}"
							value="#{mbConfiguration.selectedUser.pwd}" required="true"
							label="Password" size="50" />


						<h:outputLabel for="userName" value="User Name " />
						<p:inputText id="userName" maxlength="30"
							value="#{mbConfiguration.selectedUser.name}" required="true"
							label="User Name" size="50" />

						<h:outputLabel for="email" value="Email " />
						<p:inputText id="email"
							value="#{mbConfiguration.selectedUser.email}" required="false"
							label="Email" size="50" />


						<h:outputLabel for="enabled" value="Enabled " />
						<p:selectOneMenu value="#{mbConfiguration.selectedUser.enabled}"
							disabled="#{mbConfiguration.adminRole == false}" required="false"
							id="enabled" style="width:100px">
							<f:selectItem itemLabel="True" itemValue="true" />
							<f:selectItem itemLabel="False" itemValue="false" />
						</p:selectOneMenu>


						<h:outputLabel for="role" value="Role " />
						<p:selectOneMenu value="#{mbConfiguration.selectedUser.role}"
							disabled="#{mbConfiguration.adminRole == false}" required="true"
							id="role" style="width:100px">
							<f:selectItem itemLabel="Select One" itemValue="" />
							<f:selectItems value="#{mbConfiguration.userRoles}" />
						</p:selectOneMenu>

						<p:commandButton id="submitButton" value="Submit"
							action="#{mbConfiguration.saveUser}"
							update=":userForm:userTablePanel, :growl "
							oncomplete="handleUserRequest(xhr, status, args)" />
					</h:panelGrid>
				</h:form>
			</p:dialog>
		</ui:define>
	</ui:composition>

</h:body>
</html>